import {observable, action, configure, runInAction} from 'mobx'
import { getTree, getArticleDetail } from './CategoryServ.js'

configure({
	enforceActions: 'observed'
})


const dataList = [];
const generateList = (data) => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.title;
    dataList.push({ key, title: key });
    if (node.children) {
      generateList(node.children, node.key);
    }
  }
};


class Category {
	@observable gData = []
  @observable dataList = []
  @observable expandedKeys= []
  @observable searchValue= ''
  @observable autoExpandParent= true

  @observable articleDetail = {
    articleTitle: '',
    articleTime: '',
    articleTag: '',
    articleDesc: '',
    articleContent: ''
  }

  @action
  doGetTree = () =>  {
  	getTree().then(res => {
  		generateList(res.data);
  		runInAction(() => {
  			this.gData = res.data
  			this.dataList = dataList
  		})
  	})
  }
  
  @action
  doGetArticleDetail = (title) => {
    getArticleDetail({articleTitle: title}).then(res => {
      let {articleTitle, articleTime, articleTag, articleDesc, articleContent} = res.data
      runInAction(() => {
        this.articleDetail.articleTitle = articleTitle
        this.articleDetail.articleTime = articleTime
        this.articleDetail.articleTag = articleTag
        this.articleDetail.articleDesc = articleDesc
        this.articleDetail.articleContent = articleContent
      })
    })
  }


}

export default Category